""" Configurable. Run once then poison channels. """
from common import *

import random

default_data = ((0.0,0.0),(1.0,-1.0), 10000)

def setup(data = default_data):
    import wx
    dlg = wx.TextEntryDialog(
        None, 'Enter ((pos_x, pos_y), (angle_x, angle_y), iterations)',
        'Configure PythonCanon', str(data))
    if dlg.ShowModal() == wx.ID_OK:
        try:
            data = eval(dlg.GetValue())
        except:
            data = dlg.GetValue()
    dlg.Destroy()
    return data

def PythonCanonFunc(in0,out0 , data = default_data):
    image = in0()

    xsize = len(image[0])
    ysize = len(image)
    
    energy = [(xsize)*[0] for i in range(ysize)]
    (pos, angle, pcnt) = data
    
    for i in range(pcnt):
        x,y=pos
        ax,ay = angle
        sx=random.normalvariate(.0,0.15) #Source emmits in a normal-distributed
        sy=random.normalvariate(.0,0.15) #angle from its direction andle
        dx = ax+sx
        dy = ay+sy

        done=False
        while x>=0 and y>=0 and x<xsize and y<ysize and not done:
            #The next line says 'deposit energy with random prob
            #depending on the density of matter
            if random.randint(0,10)*(256-image[y][x])>1700:
                ix=int(x)
                iy=int(y)
                energy[iy][ix]=energy[iy][ix]+1
                done=True
            x=x+dx
            y=y+dy

    out0(energy)
